package com.epam.task.web.db;

import com.epam.task.web.util.logging.LoggerUtil;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import org.apache.log4j.Logger;

import java.sql.SQLException;
import java.sql.SQLWarning;

public class ConnectionToDB {

    LoggerUtil logger = new LoggerUtil(Logger.getLogger(ConnectionToDB.class));
    private java.sql.Connection connection;
    private boolean inUse;
    private long timeOpened;
    private long timeClosed;

    public ConnectionToDB(java.sql.Connection connection)
            throws SQLException {
        this.connection = connection;
        this.inUse = false;
        this.connection.setAutoCommit(false);
    }

    public java.sql.Connection getConnection() {
        return connection;
    }

    public void setConnection(java.sql.Connection connection) {
        this.connection = connection;
    }

    public boolean isInUse() {
        return inUse;
    }

    public void setInUse(boolean inUse) {
        this.inUse = inUse;
    }

    public long getTimeOpened() {
        return timeOpened;
    }

    public void setTimeOpened(long timeOpened) {
        this.timeOpened = timeOpened;
    }

    public long getTimeClosed() {
        return timeClosed;
    }

    public void setTimeClosed(long timeClosed) {
        this.timeClosed = timeClosed;
    }

    public boolean use() {
        if (this.inUse) {
            return false;
        } else {
            try {
                if (!this.connection.isClosed() &&
                        (this.connection.getWarnings() == null)) {
                    this.inUse = true;
                    this.timeOpened = System.currentTimeMillis();
                    return true;
                } else {
                    return false;
                }
            } catch (SQLException e) {
                logger.getExceptionTextFileLogger().error(e);
                return false;
            }
        }
    }

    public void expire() {
        try {
            this.connection.close();
            this.connection = null;
        } catch (SQLException e) {
        }
    }

    public void close() throws SQLException {
        if (this.inUse) {
            this.timeClosed = System.currentTimeMillis();
            this.inUse = false;

            if (getAutoCommit() == false) {
                setAutoCommit(true);
            }
        }
    }

    public Statement createStatement() throws SQLException {
        return (Statement) this.connection.createStatement();
    }

    public PreparedStatement prepareStatement(String query) throws SQLException {
        return (PreparedStatement) this.connection.prepareStatement(query);
    }

    public void commit() throws SQLException {
        this.connection.commit();
    }

    public boolean getAutoCommit() throws SQLException {
        return this.connection.getAutoCommit();
    }

    public void setAutoCommit(boolean autoCommit) throws SQLException {
        this.connection.setAutoCommit(autoCommit);
    }

    public void rollback() throws SQLException {
        this.connection.rollback();
    }

    public boolean isClosed() throws SQLException {
        return this.connection.isClosed();
    }

    public SQLWarning getWarnings() throws SQLException {
        return this.connection.getWarnings();
    }

}
